package SMSGUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileNameExtensionFilter;

import jxl.read.biff.BiffException;

public class Jframe {
	public static void main(String[] args) throws IOException {
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		final JFrame frame = new JFrame();
		frame.setSize(600, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		final Dimension scrnsize = toolkit.getScreenSize();
		final JFileChooser fc = new JFileChooser();
		frame.setBounds((int) ((float) .3 * scrnsize.width),
				(int) ((float) 0.3 * scrnsize.height), 550, 450);
		frame.setLayout(null);
		JButton nieuwButton, openButton;
		JMenuBar bar = new JMenuBar();

		// File Menu, F - Mnemonic
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);

		JMenuItem exit = new JMenuItem("Exit");
		exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
				ActionEvent.ALT_MASK));
		// exit.setBounds(2, 10, 40, 20);
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				System.exit(0);
			}
		});
		fileMenu.add(exit);
		bar.add(fileMenu);

		// Edit Menu, E - Mnemonic

		// Move help menu to right side
		bar.add(Box.createHorizontalGlue());

		// Help Menu, H - Mnemonic
		JMenu helpMenu = new JMenu("Help");
		JMenuItem about = new JMenuItem("About");
		about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
				ActionEvent.ALT_MASK));

		about.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				JOptionPane.showMessageDialog(frame, "This is the about Menu");
			}
		});
		helpMenu.add(about);
		helpMenu.setMnemonic(KeyEvent.VK_H);

		bar.add(helpMenu);
		frame.setJMenuBar(bar);

		final JMenuBar finalbar = bar;

		// single.getContentPane().setBackground(kleur.brighter());

		frame.setVisible(true);
		// set filter for xls only
		FileNameExtensionFilter filter = new FileNameExtensionFilter(
				".XLS Files", "xls");
		fc.setFileFilter(filter);

		JLabel welcome = new JLabel("Welkom");
		welcome.setFont(new Font("Serif", Font.BOLD, 20));
		welcome.setBounds(10, 40, 600, 50);
		frame.add(welcome);

		JLabel kiesdan = new JLabel("Openen of nieuw?");
		kiesdan.setFont(new Font("Serif", Font.BOLD, 14));
		kiesdan.setBounds(150, 100, 600, 50);
		frame.add(kiesdan);
		openButton = new JButton("Train");
		openButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int returnVal = fc.showOpenDialog(frame);

				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();

					// This is where a real application would open the file.
					try {
						String[] data;
						HashMap<Integer, String[]> excelData = ReadExcel.readFile(file, 9, 4);
						data = excelData.get(0);
						try {
							Integer.parseInt(data[1]);

							System.out.println("fileOpened");
							trainStuff.train(excelData);
							

							closeFrame(frame);
						} catch (Exception e1) {
							System.out.println("file has not the right format");
							e1.printStackTrace();
						}
					} catch (IOException e1) {
						System.out.println("Error opening file...");
					} 
				}
			}
		});

		// Create the save button. We use the image from the JLF
		// Graphics Repository (but we extracted it from the jar).
		nieuwButton = new JButton("Start nieuwe Klassificatie");
		nieuwButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				HashMap<Integer, String[]> excelData = null;
				int returnVal = fc.showOpenDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();

					// file, tweet, sentiment, name, province, city
					try {
						String[] data;
						excelData = ReadExcel.readFiveColumns(file, 9, 4, 11,
								15, 16);
						data = excelData.get(0);
						try {
							Integer.parseInt(data[1]);

							System.out.println("fileOpened");
							classifyData.classifingData(excelData);

							closeFrame(frame);
						} catch (Exception e1) {
							System.out.println("file has not the right format");
							e1.printStackTrace();
						}
					} catch (IOException e1) {
						System.out.println("Error opening file...");
					} catch (BiffException e2) {
						System.out.println("Could not regonize the file type");
					}

				} else {

				}
			}
		});

		nieuwButton.setBounds(50, 300, 200, 40);
		openButton.setBounds(300, 300, 200, 40);
		frame.add(nieuwButton);
		frame.add(openButton);

	}

	public static void closeFrame(JFrame frame) {
		frame.dispose();
		frame.setVisible(false);
	}
}
